{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45449af8",
   "metadata": {},
   "source": [
    "### 3.1 使用ChatGPT学习数据抽样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da6a446a",
   "metadata": {},
   "source": [
    "#### 3.1.2\t数据简单随机抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3c566890",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机抽样结果: [9, 3, 5]\n"
     ]
    }
   ],
   "source": [
    "# 无放回抽样\n",
    "import random\n",
    "# 示例数据集\n",
    "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "# 进行随机抽样，选择3个样本\n",
    "sampled_data = random.sample(data, 3)\n",
    "print(\"随机抽样结果:\", sampled_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0021c064",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机抽样结果: [5, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "# 示例数据集\n",
    "data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "# 使用权重进行抽样\n",
    "weights = [0.1, 0.2, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]\n",
    "\n",
    "# 进行随机抽样，选择3个样本\n",
    "sampled_data = random.choices(data, weights=weights, k=3)\n",
    "\n",
    "print(\"随机抽样结果:\", sampled_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7a5fac76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choices(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "74d4d34f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 2, 5, 2, 6, 5, 8, 1, 1, 7, 3, 10, 7, 7, 5]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choices(data,k=15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c953f50",
   "metadata": {},
   "source": [
    "#### 3.1.3\t数据分区"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9f05090f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 加载Iris数据集\n",
    "iris = load_iris()\n",
    "X = iris.data\n",
    "y = iris.target\n",
    "\n",
    "# 使用train_test_split进行随机抽样，测试集比例为20%，random_state设置为固定值（例如，42）\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 现在，X_train 和 y_train 包含训练数据，X_test 和 y_test 包含测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e9bf575d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查看X_train的占比： 0.8\n",
      "查看y_train的占比： 0.8\n"
     ]
    }
   ],
   "source": [
    "print('查看X_train的占比：',len(X_train) / len(X))\n",
    "print('查看y_train的占比：',len(y_train) / len(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0c1c14c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查看y各类别占比：\n",
      " 0    0.333333\n",
      "1    0.333333\n",
      "2    0.333333\n",
      "dtype: float64\n",
      "查看y_trian各类别占比：\n",
      " 1    0.341667\n",
      "0    0.333333\n",
      "2    0.325000\n",
      "dtype: float64\n",
      "查看y_test各类别占比：\n",
      " 2    0.366667\n",
      "0    0.333333\n",
      "1    0.300000\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# 查看y、y_train、y_test各类别占比\n",
    "print('查看y各类别占比：\\n',pd.value_counts(y) / len(y))\n",
    "print('查看y_trian各类别占比：\\n',pd.value_counts(y_train) / (len(y)*0.8))\n",
    "print('查看y_test各类别占比：\\n',pd.value_counts(y_test) / (len(y)*0.2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47b5b74a",
   "metadata": {},
   "source": [
    "#### 3.1.4\t分层抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b13b971a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用train_test_split按照y进行分层抽样，测试集比例为20%，random_state设置为固定值（例如，42）,\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42,stratify=y )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "919d66a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查看y各类别占比：\n",
      " 0    0.333333\n",
      "1    0.333333\n",
      "2    0.333333\n",
      "dtype: float64\n",
      "查看y_trian各类别占比：\n",
      " 0    0.333333\n",
      "2    0.333333\n",
      "1    0.333333\n",
      "dtype: float64\n",
      "查看y_test各类别占比：\n",
      " 0    0.333333\n",
      "2    0.333333\n",
      "1    0.333333\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 查看y、y_train、y_test各类别占比\n",
    "print('查看y各类别占比：\\n',pd.value_counts(y) / len(y))\n",
    "print('查看y_trian各类别占比：\\n',pd.value_counts(y_train) / (len(y)*0.8))\n",
    "print('查看y_test各类别占比：\\n',pd.value_counts(y_test) / (len(y)*0.2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e11f47c",
   "metadata": {},
   "source": [
    "##### 使用StratifiedShuffleSplit函数实现分层抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "42b422fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import StratifiedShuffleSplit\n",
    "\n",
    "# 加载Iris数据集\n",
    "iris = load_iris()\n",
    "X = iris.data\n",
    "y = iris.target\n",
    "\n",
    "# 创建 StratifiedShuffleSplit 对象\n",
    "sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)\n",
    "\n",
    "# 使用 split 方法进行分层随机抽样\n",
    "for train_index, test_index in sss.split(X, y):\n",
    "    X_train, X_test = X[train_index], X[test_index]\n",
    "    y_train, y_test = y[train_index], y[test_index]\n",
    "\n",
    "# 现在，X_train 和 y_train 包含训练数据，X_test 和 y_test 包含测试数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "740ca16d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查看y各类别占比：\n",
      " 0    0.333333\n",
      "1    0.333333\n",
      "2    0.333333\n",
      "dtype: float64\n",
      "查看y_trian各类别占比：\n",
      " 0    0.333333\n",
      "2    0.333333\n",
      "1    0.333333\n",
      "dtype: float64\n",
      "查看y_test各类别占比：\n",
      " 0    0.333333\n",
      "2    0.333333\n",
      "1    0.333333\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 查看y、y_train、y_test各类别占比\n",
    "print('查看y各类别占比：\\n',pd.value_counts(y) / len(y))\n",
    "print('查看y_trian各类别占比：\\n',pd.value_counts(y_train) / (len(y)*0.8))\n",
    "print('查看y_test各类别占比：\\n',pd.value_counts(y_test) / (len(y)*0.2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98cbab00",
   "metadata": {},
   "source": [
    "#### 3.1.5 数据类失衡处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6a439fb",
   "metadata": {},
   "source": [
    "##### 过采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "85e51729",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original dataset shape Counter({1: 900, 0: 100})\n",
      "Resampled dataset shape Counter({0: 900, 1: 900})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "from sklearn.datasets import make_classification\n",
    "from imblearn.over_sampling import SMOTE \n",
    "X, y = make_classification(n_classes=2, class_sep=2,\n",
    "                           weights=[0.1, 0.9], n_informative=3, n_redundant=1, flip_y=0,\n",
    "                           n_features=20, n_clusters_per_class=1, n_samples=1000, random_state=10)\n",
    "print('Original dataset shape %s' % Counter(y))\n",
    "sm = SMOTE(random_state=42)\n",
    "X_res, y_res = sm.fit_resample(X, y)\n",
    "print('Resampled dataset shape %s' % Counter(y_res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "e9233dec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resampled dataset shape Counter({1: 900, 0: 450})\n"
     ]
    }
   ],
   "source": [
    "# 过采样后少数类别为多数类别的一半\n",
    "sm1 = SMOTE(sampling_strategy=0.5,random_state=42)\n",
    "X_res1, y_res1 = sm1.fit_resample(X, y)\n",
    "print('Resampled dataset shape %s' % Counter(y_res1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f3e35bf",
   "metadata": {},
   "source": [
    "#### 欠采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "2f1a3b2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from imblearn.under_sampling import RandomUnderSampler\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "ad01e6c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resampled dataset shape Counter({0: 100, 1: 100})\n"
     ]
    }
   ],
   "source": [
    "rus = RandomUnderSampler(random_state=42)\n",
    "X_rus, y_rus = rus.fit_resample(X, y)\n",
    "print('Resampled dataset shape %s' % Counter(y_rus))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "853a6954",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resampled dataset shape Counter({1: 200, 0: 100})\n"
     ]
    }
   ],
   "source": [
    "rus1 = RandomUnderSampler(sampling_strategy=0.5,random_state=42)\n",
    "X_rus1, y_rus1 = rus.fit_resample(X, y)\n",
    "print('Resampled dataset shape %s' % Counter(y_rus1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fbbba24",
   "metadata": {},
   "source": [
    "### 3.2 使用ChatGPT学习数据清洗"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56443e9d",
   "metadata": {},
   "source": [
    "#### 3.2.1\t缺失值识别及处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3bcb4c27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A    B\n",
       "0  1.0  NaN\n",
       "1  2.0  2.0\n",
       "2  NaN  3.0\n",
       "3  4.0  NaN\n",
       "4  5.0  6.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 识别缺失值\n",
    "import pandas as pd\n",
    "data = pd.DataFrame({'A': [1, 2, None, 4, 5],\n",
    "                     'B': [None, 2, 3, None, 6]})\n",
    "\n",
    "# 查看数据框data\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80e2d17e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       A      B\n",
       "0  False   True\n",
       "1  False  False\n",
       "2   True  False\n",
       "3  False   True\n",
       "4  False  False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# isnull()或isna()使用检测缺失值\n",
    "data.isnull() # 等价于data.isna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3d151617",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       A      B\n",
       "0   True  False\n",
       "1   True   True\n",
       "2  False   True\n",
       "3   True  False\n",
       "4   True   True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# notnull()或notna()使用检测缺失值\n",
    "data.notnull() # 等价于data.notna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f43ec823",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A    B\n",
      "1  2.0  2.0\n",
      "4  5.0  6.0\n"
     ]
    }
   ],
   "source": [
    "# 删除缺失值\n",
    "import pandas as pd\n",
    "\n",
    "data = pd.DataFrame({'A': [1, 2, None, 4, 5],\n",
    "                     'B': [None, 2, 3, None, 6]})\n",
    "\n",
    "# 删除包含缺失值的行\n",
    "data_cleaned = data.dropna()\n",
    "print(data_cleaned)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "20b5ecdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "查看原始数据:\n",
      "    A    B\n",
      "0  1  NaN\n",
      "1  2  2.0\n",
      "2  4  3.0\n",
      "查看按列删除后数据:\n",
      "    A\n",
      "0  1\n",
      "1  2\n",
      "2  4\n"
     ]
    }
   ],
   "source": [
    "data1 = pd.DataFrame({'A': [1, 2, 4],\n",
    "                     'B': [None, 2, 3]})\n",
    "drop_col = data1.dropna(axis=1) # 按列删除\n",
    "print('查看原始数据:\\n',data1)\n",
    "print('查看按列删除后数据:\\n',drop_col)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0e782d6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A    B\n",
      "0  1.0  0.0\n",
      "1  2.0  2.0\n",
      "2  0.0  3.0\n",
      "3  4.0  0.0\n",
      "4  5.0  6.0\n"
     ]
    }
   ],
   "source": [
    "# 缺失值填充\n",
    "import pandas as pd\n",
    "\n",
    "data = pd.DataFrame({'A': [1, 2, None, 4, 5],\n",
    "                     'B': [None, 2, 3, None, 6]})\n",
    "\n",
    "# 用指定值（例如0）填充缺失值\n",
    "data_filled = data.fillna(0)\n",
    "\n",
    "print(data_filled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "63e64ce4",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      "      A    B\n",
      "0  1.0  NaN\n",
      "1  2.0  2.0\n",
      "2  NaN  3.0\n",
      "3  4.0  NaN\n",
      "4  5.0  6.0\n",
      "用前一个非缺失值来填充缺失值的方式:\n",
      "      A    B\n",
      "0  1.0  NaN\n",
      "1  2.0  2.0\n",
      "2  2.0  3.0\n",
      "3  4.0  3.0\n",
      "4  5.0  6.0\n",
      "用后一个非缺失值来填充缺失值的方式:\n",
      "      A    B\n",
      "0  1.0  2.0\n",
      "1  2.0  2.0\n",
      "2  4.0  3.0\n",
      "3  4.0  6.0\n",
      "4  5.0  6.0\n",
      "用各列均值填充缺失值的方式:\n",
      "      A         B\n",
      "0  1.0  3.666667\n",
      "1  2.0  2.000000\n",
      "2  3.0  3.000000\n",
      "3  4.0  3.666667\n",
      "4  5.0  6.000000\n"
     ]
    }
   ],
   "source": [
    "# 其它常用的缺失值填充方式\n",
    "# 1. method='pad'或'ffill': 用前一个非缺失值来填充缺失值。\n",
    "fill_front = data.fillna(method='pad')\n",
    "# 2. method='bfill'或'backfill': 用后一个非缺失值来填充缺失值。\n",
    "fill_back = data.fillna(method='bfill')\n",
    "# 3. 用各列均值填充缺失值\n",
    "fill_mean = data.fillna(data.mean())\n",
    "# 输出各种结果\n",
    "print('原始数据:\\n',data)\n",
    "print('用前一个非缺失值来填充缺失值的方式:\\n',fill_front)\n",
    "print('用后一个非缺失值来填充缺失值的方式:\\n',fill_back)\n",
    "print('用各列均值填充缺失值的方式:\\n',fill_mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46488bc2",
   "metadata": {},
   "source": [
    "### 3.3 使用ChatGPT学习数据变换"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de2a5393",
   "metadata": {},
   "source": [
    "#### 3.3.1 数据标准化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b327f77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据： [10 20 30 40 50]\n",
      "Min-Max标准化后的数据： [0.   0.25 0.5  0.75 1.  ]\n"
     ]
    }
   ],
   "source": [
    "# 使用NumPy进行Min-Max标准化\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个示例数据集\n",
    "data = np.array([10, 20, 30, 40, 50])\n",
    "\n",
    "# 使用NumPy进行Min-Max标准化\n",
    "min_value = data.min()\n",
    "max_value = data.max()\n",
    "\n",
    "normalized_data = (data - min_value) / (max_value - min_value)\n",
    "\n",
    "print(\"原始数据：\", data)\n",
    "print(\"Min-Max标准化后的数据：\", normalized_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "da641456",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z-score标准化后的数据： [-1.41421356 -0.70710678  0.          0.70710678  1.41421356]\n"
     ]
    }
   ],
   "source": [
    "# 使用NumPy进行Min-Max标准化\n",
    "mean_value = data.mean()\n",
    "sd_value = data.std()\n",
    "scale_data = (data - mean_value) / sd_value\n",
    "print(\"Z-score标准化后的数据：\", scale_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8241a99e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n",
      " [[10]\n",
      " [20]\n",
      " [30]\n",
      " [40]\n",
      " [50]]\n",
      "Z-Score标准化后的数据：\n",
      " [[-1.41421356]\n",
      " [-0.70710678]\n",
      " [ 0.        ]\n",
      " [ 0.70710678]\n",
      " [ 1.41421356]]\n"
     ]
    }
   ],
   "source": [
    "# 使用scikit-learn进行Z-Score标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 创建一个示例数据集\n",
    "data = np.array([10, 20, 30, 40, 50]).reshape(-1, 1)\n",
    "\n",
    "# 使用scikit-learn进行Z-Score标准化\n",
    "scaler = StandardScaler()\n",
    "normalized_data = scaler.fit_transform(data)\n",
    "\n",
    "print(\"原始数据：\\n\", data)\n",
    "print(\"Z-Score标准化后的数据：\\n\", normalized_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "336de59b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n",
      " [[10]\n",
      " [20]\n",
      " [30]\n",
      " [40]\n",
      " [50]]\n",
      "Min-Max标准化后的数据：\n",
      " [[0.  ]\n",
      " [0.25]\n",
      " [0.5 ]\n",
      " [0.75]\n",
      " [1.  ]]\n"
     ]
    }
   ],
   "source": [
    "# 使用scikit-learn进行Min-Max标准化\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "# 创建MinMaxScaler对象\n",
    "scaler = MinMaxScaler()\n",
    "# 使用MinMaxScaler进行Min-Max标准化\n",
    "normalized_data = scaler.fit_transform(data)\n",
    "print(\"原始数据：\\n\", data)\n",
    "print(\"Min-Max标准化后的数据：\\n\", normalized_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4934f98e",
   "metadata": {},
   "source": [
    "#### 3.3.2 数据独热编码处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f636d2ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   水果_橙子  水果_苹果  水果_香蕉\n",
      "0      0      1      0\n",
      "1      1      0      0\n",
      "2      0      0      1\n",
      "3      1      0      0\n"
     ]
    }
   ],
   "source": [
    "# 使用 pandas 实现数据独热编码\n",
    "import pandas as pd\n",
    "\n",
    "# 创建一个包含分类数据的DataFrame\n",
    "data = pd.DataFrame({'水果': ['苹果', '橙子', '香蕉', '橙子']})\n",
    "\n",
    "# 使用get_dummies进行独热编码\n",
    "encoded_data = pd.get_dummies(data, columns=['水果'])\n",
    "\n",
    "print(encoded_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3a85a2f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# 使用 scikit-learn 的 OneHotEncoder\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个示例数据集\n",
    "data = np.array(['苹果', '橙子', '香蕉', '橙子']).reshape(-1, 1)\n",
    "\n",
    "# 创建OneHotEncoder对象\n",
    "encoder = OneHotEncoder(sparse=False)\n",
    "\n",
    "# 进行独热编码\n",
    "encoded_data = encoder.fit_transform(data)\n",
    "\n",
    "print(encoded_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e08eac0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
